<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Models\User;
use App\Http\Controllers\Controller;
//use Mockery\Exception;
//use Overtrue\EasySms\EasySms;
//use Illuminate\Support\Facades\Config;
//use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\DB;
//use Overtrue\EasySms\Exceptions\NoGatewayAvailableException;
use Validator;
//use Illuminate\Auth\Passwords\PasswordBroker;

class StatisticsController extends Controller
{
    private $company_id;
    private $user;
    private $role_flag;
    public function __construct()
    {
        $this->deviceType = $this->getDevice();
        if (\Auth::guard('api')->check()) {
            $user = \Auth::guard('api')->user();
            $data = User::find($user->id);
            if($this->deviceType){
//                PC
                $this->company_id = $data->company_id;
                $this->user = $data;
            }else{
//                APP
                $this->company_id = $data->appcompany_id;
                $this->user = $data;
                $this->user->company_id = $data->appcompany_id;
            }
            $company = \App\Models\Company::find($this->user->company_id);
            if(!empty($company)){
                $this->role_flag = $company->role_flag;
            }
        }
    }
//    统计
    public function statistics(Request $request){
        $userInfo = $this->user;
//        dd($userInfo);
        $query = DB::table('flow_histories')
            ->join('resumes','resumes.id','=','flow_histories.resume_id')
            ->leftJoin('positions','resumes.position_id','=','positions.id')
            ->where('flow_histories.company_id',$userInfo->company_id)
            ->where('resumes.status',0);
        $steps = $request->input('steps');
        $edu = $request->input('edu');
        $peopleId = $request->input('peopleId');
        $position = $request->input('position');
        $sex = $request->input('sex');
        if(!empty($steps)){
            if($steps==4){
                $query = $query->whereNotIn('flow_histories.step_id',[1,2,3]);
            }else{
                $query = $query->where('flow_histories.step_id',$steps);
            }
        }
        if(!empty($edu)){
            if($edu=='其他'){
                $query = $query->where(function($query){
                    $query = $query->whereNull('resumes.edu')->orWhere([['resumes.edu','!=','大专及以下'],['resumes.edu','!=','大专'],['resumes.edu','!=','本科'],['resumes.edu','!=','硕士'],['resumes.edu','!=','博士'],['resumes.edu','!=','博士及以上']]);
                    return $query;
                });
               
            }else{
                $query = $query->where('resumes.edu',$edu);
            }
        }
        if(!empty($sex)){
            if($sex=='其他'){
                $query = $query = $query->where('resumes.sex','');
            }else{
                $query = $query->where('resumes.sex',$sex);
            }
        }
        if(!empty($peopleId)){
            $query = $query->where('flow_histories.owner_id',$peopleId);
        }
        if(!empty($position)){
            $query = $query->where(['resumes.position_id'=>$position,'positions.status'=>0]);
        }
        
        $flows = $degrees = $sexs =    $query;
        $flow = $this->flow($request,$flows);
       
        $degree =$this->degree($request,$degrees);
      
        $sex = $this->sex($request, $sexs);
       
        $trendFlow = $this->trendFlow($request);
       
        $position = $this->position($request);
      
        $persion = $this->persional($request);
       
        return response()->json(['code' => 200, 'msg' => '操作成功', 'data' =>['flow'=>$flow,'degree'=>$degree,'sex'=>$sex,'trendFlow'=>$trendFlow,'position'=>$position,'persion'=>$persion]]);
    }
//        分布图
//        流程统计分布图
    public function flow(Request $request,$query){
        $time = $request->input('time');
        $now = time();
        if(!empty($time)){
            switch($time){
                case 1:
//                当天
//                    $startTime = strtotime(date('Y-m-d 00:00:00',$now));
                    $startTime = strtotime(date('Ymd'));
                    break;
                case 3:
//                本月
//                    $time = date('Y-m-d 00:00:00', mktime(0, 0, 0, date('m', $now), '1', date('Y', $now)));
                    $startTime = $now - 24 * 3600 * 30;
                    break;
                case 4:
//            三个月
//                    $time = strtotime('-2 month', $now);
//                    $beginTime = date('Y-m-d 00:00:00', mktime(0, 0,0, date('m', $time), 1, date('Y', $time)));
//                    $startTime =  strtotime($beginTime);
                    $startTime = $now - 24 * 3600 * 90;
                    break;
                case 5:
//                半年内
//                    $time = strtotime('-5 month', $now);
//                    $beginTime = date('Y-m-d 00:00:00', mktime(0, 0,0, date('m', $time), 1, date('Y', $time)));
//                    $startTime =  strtotime($beginTime);
                    $startTime = $now - 24 * 3600 * 180;
                    break;
                case 6:
//                    $beginTime = date('Y-m-d 00:00:00', mktime(0, 0,0, 1, 1, date('Y', $now)));
//                    $startTime =  strtotime($beginTime);
                    $startTime = $now - 24 * 3600 * 360;
//                一年内
                    break;
                default :
//                本周
//                    $time = '1' == date('w') ? strtotime('Monday', $now) : strtotime('last Monday', $now);
//                    $startTime = $time;
                    $startTime = $now - 24 * 3600 * 7;
                    break;
                
            }
            $query = $query->whereBetween('flow_histories.created_at',[$startTime,$now]);
        }
        $result = $query->select('flow_histories.id','flow_histories.step_id')->get();
        $flows['trial'] = $flows['into'] =  $flows['offer'] =  $flows['others'] = 0;
        foreach($result as $k=>$v){
                if($v->step_id == 1){
                    $flows['trial'] +=1;
                }else if($v->step_id == 3){
                    $flows['into'] +=1;
                }else if($v->step_id==2){
                    $flows['offer'] +=1;
                }else{
                    $flows['others'] +=1;
                }
        }
      return $flows;
        
    }
//    学历统计
    public function degree(Request $request,$query){
        $result = $query->select('resumes.id','resumes.edu')->get();
        $degree['college'] = $degree['undergraduate'] = $degree['master'] = $degree['dr'] = $degree['others']=0;
        foreach($result as $k=>$v){
            if($v->edu == '大专及以下' || $v->edu == '大专'){
                $degree['college'] +=1;
            }else if($v->edu == '本科'){
                $degree['undergraduate']+=1;
            }else if($v->edu == '硕士'){
                $degree['master'] +=1;
            }else if($v->edu == '博士' || $v->edu == '博士及以上'){
                $degree['dr']+=1;
            }else{
                $degree['others'] +=1;
            }
        }
        return $degree;
    }
//    性别统计
    public function sex(Request $request,$query){
        $result = $query->select('resumes.id','resumes.sex')->get();
        $sex['man'] = $sex['women'] = $sex['others'] = 0;
        foreach($result as $k=>$v){
            if($v->sex=='男'){
                $sex['man'] +=1;
            }else if($v->sex =='女'){
                $sex['women'] +=1;
            }else{
                $sex['others']+=1;
            }
        }
        return $sex;
    }
//    趋势图
//流程统计
    public function trendFlow(Request $request){
        $userInfo = $this->user;
        $t = strtotime(date('Y-m'));
//        dd($userInfo);
        $querys = DB::table('flow_histories')
            ->join('resumes','resumes.id','=','flow_histories.resume_id')
            ->leftJoin('positions','resumes.position_id','=','positions.id')
            ->where('flow_histories.company_id',$userInfo->company_id)
            ->where('resumes.status',0);
            
        $steps = $request->input('steps');
        $edu = $request->input('edu');
        $peopleId = $request->input('peopleId');
        $position = $request->input('position');
        $sex = $request->input('sex');
        if(!empty($steps)){
            if($steps==4){
                $querys = $querys->whereNotIn('flow_histories.step_id',[1,2,3]);
            }else{
                $querys = $querys->where('flow_histories.step_id',$steps);
            }
        }
        if(!empty($edu)){
            if($edu=='其他'){
                $querys = $querys->where(function($query){
                    $querys = $query->whereNull('resumes.edu')->orWhere([['resumes.edu','!=','大专及以下'],['resumes.edu','!=','大专'],['resumes.edu','!=','本科'],['resumes.edu','!=','硕士'],['resumes.edu','!=','博士'],['resumes.edu','!=','博士及以上']]);
                    return $querys;
                });
            
            }else{
                $querys = $querys->where('resumes.edu',$edu);
            }
        }
        if(!empty($sex)){
            if($sex=='其他'){
                $querys =  $querys->where('resumes.sex','');
            }else{
                $querys = $querys->where('resumes.sex',$sex);
            }
        }
        if(!empty($peopleId)){
            $querys = $querys->where('flow_histories.owner_id',$peopleId);
        }
        if(!empty($position)){
            $querys = $querys->where(['resumes.position_id'=>$position,'positions.status'=>0]);
        }
        $time = $request->input('time') ?? 2;
        $now = time();
            switch($time) {
                case 1:
                    //当天
                    $starttime = strtotime(date('Ymd'));
                    $endtime = time();
                    $result = $querys
                        ->select(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m/%d') as hours,flow_histories.step_id"), DB::raw("count(*) as count"))
                        ->whereBetween('flow_histories.created_at', [$starttime, $endtime])
                        ->groupBy(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m/%d'),flow_histories.step_id"))
                        ->get()
                        ->toarray();
                    $list['day'][0] = date('Y/m/d',$now);
                    $list['number']['trial'] =$list['number']['into'] =$list['number']['offer']=$list['number']['others']=[0];
                    if(empty($result)){
                        $list['number']['trial'][0] =$list['number']['into'][0] =$list['number']['offer'][0]=$list['number']['others'][0]=0;
                    }else{
                       foreach($result as $k){
                           if($k->step_id == 1){
                               $list['number']['trial'][0] = $k->count;
                           }elseif($k->step_id == 3){
                               $list['number']['into'][0] = $k->count;
                           }elseif ($k->step_id == 2){
                               $list['number']['offer'][0] =  $k->count;
                           }else{
                               $list['number']['others'][0] += $k->count;
                           }
                        }
                    }
                    return $list;
                    break;
                //30天
                case 3:
                    $endtime = time();
                    $starttime = $endtime - 24 * 3600 * 30;
                    $result = $querys
                        ->select(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m/%d') as hours,flow_histories.step_id"), DB::raw("count(*) as count"))
                        ->whereBetween('flow_histories.created_at', [$starttime, $endtime])
                        ->groupBy(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m/%d'),flow_histories.step_id"))
                        ->get()
                        ->toarray();
                    for ($i = 0; $i < 30; $i++) {
                        $h = 29-$i;
                        $list['day'][$i] = date('Y/m/d', strtotime("-$h day"));
                        $list['number']['trial'][$i] = $list['number']['into'][$i]=
                        $list['number']['offer'][$i]=$list['number']['others'][$i]=0;
                    }
                    if (!empty( $result)) {
                        foreach ( $result as $k) {
                            if (in_array($k->hours, $list['day'])) {
                                $key = array_search($k->hours,$list['day']);
                                if($k->step_id == 1){
                                    $list['number']['trial'][$key] = $k->count;
                                }elseif($k->step_id == 3){
                                    $list['number']['into'][$key] = $k->count;
                                }elseif ($k->step_id == 2){
                                    $list['number']['offer'][$key] =  $k->count;
                                }else{
                                    $list['number']['others'][$key] += $k->count;
                                }
                            }
                        }
                    }
                    $a=$b=$c=$s = -1;
                    
                    for ($i = 0; $i < 10; $i++) {
                        $h = 30 - $i * 3;
                        $k = 27 - $i * 3;
                        $listInfo['day'][$i] = date("m/d", strtotime("-$h day")) . '-' . date("m/d", strtotime("-$k day"));
                        $listInfo['number']['trial'][$i] = $list['number']['trial'][++$s] + $list['number']['trial'][++$s] + $list['number']['trial'][++$s];
                        $listInfo['number']['into'][$i] = $list['number']['into'][++$a] + $list['number']['into'][++$a] + $list['number']['into'][++$a];
                        $listInfo['number']['offer'][$i] = $list['number']['offer'][++$b] + $list['number']['offer'][++$b] + $list['number']['offer'][++$b];
                        $listInfo['number']['others'][$i] = $list['number']['others'][++$c] + $list['number']['others'][++$c] + $list['number']['others'][++$c];
                    }
                    return $listInfo;
                    break;
                //90天
                case 4:
                    $endtime = time();
                    $starttime = $endtime - 24 * 3600 * 90;
                    $result = $querys
                        ->select(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m/%d') as hours,flow_histories.step_id"), DB::raw("count(*) as count"))
                        ->whereBetween('flow_histories.created_at', [$starttime, $endtime])
                        ->groupBy(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m/%d'),flow_histories.step_id"))
                        ->get()
                        ->toarray();
                    for ($i = 0; $i < 90; $i++) {
                        $h = 89-$i;
                        $list['day'][$i] = date('Y/m/d', strtotime("-$h day"));
                        $list['number']['trial'][$i] = $list['number']['into'][$i]=
                        $list['number']['offer'][$i]=$list['number']['others'][$i]=0;
                    }
                    if (!empty($result)) {
                        foreach ($result as $k) {
                            if (in_array($k->hours, $list['day'])) {
                                $key = array_search($k->hours,$list['day']);
                                if($k->step_id == 1){
                                    $list['number']['trial'][$key] = $k->count;
                                }elseif($k->step_id == 3){
                                    $list['number']['into'][$key] = $k->count;
                                }elseif ($k->step_id == 2){
                                    $list['number']['offer'][$key] =  $k->count;
                                }else{
                                    $list['number']['others'][$key] += $k->count;
                                }
                            }
                        }
                    }
                    $a=$b=$c=$s = -1;
                    for ($i = 0; $i < 10; $i++) {
                        $h = 90 - $i * 9;
                        $k = 81 - $i * 9;
                        $listInfo['day'][$i] = date("m/d", strtotime("-$h day")) . '-' . date("m/d", strtotime("-$k day"));
                        $listInfo['number']['trial'][$i] =
                            $list['number']['trial'][++$s] +  $list['number']['trial'][++$s] +
                            $list['number']['trial'][++$s] +  $list['number']['trial'][++$s] +
                            $list['number']['trial'][++$s] +  $list['number']['trial'][++$s] +
                            $list['number']['trial'][++$s] +  $list['number']['trial'][++$s] +
                            $list['number']['trial'][++$s];
                        $listInfo['number']['into'][$i] =  $list['number']['into'][++$a]+ $list['number']['into'][++$a]+ $list['number']['into'][++$a]+ $list['number']['into'][++$a]+ $list['number']['into'][++$a]+ $list['number']['into'][++$a]+ $list['number']['into'][++$a]+ $list['number']['into'][++$a]+ $list['number']['into'][++$a];
                        $listInfo['number']['offer'][$i] =   $list['number']['offer'][++$b] + $list['number']['offer'][++$b] + $list['number']['offer'][++$b] + $list['number']['offer'][++$b] + $list['number']['offer'][++$b] + $list['number']['offer'][++$b] + $list['number']['offer'][++$b] + $list['number']['offer'][++$b] + $list['number']['offer'][++$b] ;
                        $listInfo['number']['others'][$i] =  $list['number']['others'][++$c]+ $list['number']['others'][++$c]+ $list['number']['others'][++$c]+ $list['number']['others'][++$c]+ $list['number']['others'][++$c]+ $list['number']['others'][++$c]+ $list['number']['others'][++$c]+ $list['number']['others'][++$c]+ $list['number']['others'][++$c];
                    }
                    return $listInfo;
                    break;
                //180天
                case 5:
                    $endtime = time();
                    $starttime = $endtime - 24 * 3600 * 180;
                    $result = $querys
                        ->select(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m') as hours,flow_histories.step_id"), DB::raw("count(*) as count"))
                        ->whereBetween('flow_histories.created_at', [$starttime, $endtime])
                        ->groupBy(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m'),flow_histories.step_id"))
                        ->get()
                        ->toarray();
                   
                    for ($i = 0; $i < 6; $i++) {
                        $h = 5-$i;
                        $list['day'][$i] = date('Y/m', strtotime("-$h month",$t));
                        $list['number']['trial'][$i] = $list['number']['into'][$i]=
                        $list['number']['offer'][$i]=$list['number']['others'][$i]=0;
                    }
                    
                    if (!empty($result)) {
                        foreach ($result as $k) {
                            if (in_array($k->hours, $list['day'])) {
                                $key = array_search($k->hours,$list['day']);
                                if($k->step_id == 1){
                                    $list['number']['trial'][$key] = $k->count;
                                }elseif($k->step_id == 3){
                                    $list['number']['into'][$key] = $k->count;
                                }elseif ($k->step_id == 2){
                                    $list['number']['offer'][$key] =  $k->count;
                                }else{
                                    $list['number']['others'][$key] += $k->count;
                                }
                            }
                        }
                    }
                    return $list;
                    break;
                //360天
                case 6:
                    $endtime = time();
                    $starttime = $endtime - 24 * 3600 * 360;
                    $result = $querys
                        ->select(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m') as hours,flow_histories.step_id"), DB::raw("count(*) as count"))
                        ->whereBetween('flow_histories.created_at', [$starttime, $endtime])
                        ->groupBy(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m'),flow_histories.step_id"))
                        ->get()
                        ->toarray();
                    for ($i = 0; $i < 12; $i++) {
                        $h = 11-$i;
                        $list['day'][$i] = date('Y/m', strtotime("- $h month",$t));
                        $list['number']['trial'][$i] = $list['number']['into'][$i]=
                        $list['number']['offer'][$i]=$list['number']['others'][$i]=0;
                    }
                    if (!empty($result)) {
                        foreach ($result as $k) {
                            if (in_array($k->hours, $list['day'])) {
                                $key = array_search($k->hours,$list['day']);
                                if($k->step_id == 1){
                                    $list['number']['trial'][$key] = $k->count;
                                }elseif($k->step_id == 3){
                                    $list['number']['into'][$key] = $k->count;
                                }elseif ($k->step_id == 2){
                                    $list['number']['offer'][$key] =  $k->count;
                                }else{
                                    $list['number']['others'][$key] += $k->count;
                                }
                            }
                        }
                    }
                    return $list;
                    break;
               
                //一周
                default:
                    $endtime = time();
                    $starttime = $endtime - 24 * 3600 * 7;
                    $result = $querys
                        ->select(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m/%d') as hours,flow_histories.step_id"), DB::raw("count(*) as count"))
                        ->whereBetween('flow_histories.created_at', [$starttime, $endtime])
                        ->groupBy(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m/%d'),flow_histories.step_id"))
                        ->get()
                        ->toarray();
                    for ($i = 0; $i < 7; $i++) {
                        $h = 6-$i;
                        $list['day'][$i] = date('Y/m/d', strtotime("-$h day"));
                    }
                    $list['number']['trial'] = [0,0,0,0,0,0,0];
                    $list['number']['into'] = [0,0,0,0,0,0,0];
                    $list['number']['offer'] = [0,0,0,0,0,0,0];
                    $list['number']['others'] = [0,0,0,0,0,0,0];
                    if (!empty($result)) {
                        foreach ($result as $k) {
                            if (in_array($k->hours, $list['day'])) {
                                $key = array_search($k->hours,$list['day']);
                                if($k->step_id == 1){
                                    $list['number']['trial'][$key] = $k->count;
                                }elseif($k->step_id == 3){
                                    $list['number']['into'][$key] = $k->count;
                                }elseif ($k->step_id == 2){
                                    $list['number']['offer'][$key] =  $k->count;
                                }else{
                                    $list['number']['others'][$key] += $k->count;
                                }
                            }
                        }
                    }
                    return $list;
                    break;
            }
            
    }
//    职位统计
    public function position(Request $request){
        $userInfo = $this->user;
        $t = strtotime(date('Y-m'));
//        dd($userInfo);
        $queryss = DB::table('flow_histories')
            ->join('resumes','resumes.id','=','flow_histories.resume_id')
            ->leftJoin('positions','resumes.position_id','=','positions.id')
            ->where('flow_histories.company_id',$userInfo->company_id)
            ->where('resumes.status',0);
            
        $steps = $request->input('steps');
        $edu = $request->input('edu');
        $peopleId = $request->input('peopleId');
        $position = $request->input('position');
        $sex = $request->input('sex');
        if(!empty($steps)){
            if($steps==4){
                $queryss = $queryss->whereNotIn('flow_histories.step_id',[1,2,3]);
            }else{
                $queryss = $queryss->where('flow_histories.step_id',$steps);
            }
        }
        if(!empty($edu)){
            if($edu=='其他'){
                $queryss = $queryss->where(function($query){
                    $queryss = $query->whereNull('resumes.edu')->orWhere([['resumes.edu','!=','大专及以下'],['resumes.edu','!=','大专'],['resumes.edu','!=','本科'],['resumes.edu','!=','硕士'],['resumes.edu','!=','博士'],['resumes.edu','!=','博士及以上']]);
                    return $queryss;
                });
            
            }else{
                $queryss = $queryss->where('resumes.edu',$edu);
            }
        }
        if(!empty($sex)){
            if($sex=='其他'){
                $queryss = $queryss->where('resumes.sex','');
            }else{
                $queryss = $queryss->where('resumes.sex',$sex);
            }
        }
        if(!empty($peopleId)){
            $queryss = $queryss->where('flow_histories.owner_id',$peopleId);
        }
        if(!empty($position)){
            $queryss = $queryss->where(['resumes.position_id'=>$position,'positions.status'=>0]);
        }
        $time = $request->input('time') ?? 2;
        $now = time();
            switch($time) {
                case 1:
                    //当天
                    $starttime = strtotime(date('Ymd'));
                    $endtime = time();
                    $result = $queryss
                        ->select(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m/%d') as hours"), DB::raw("count(*) as count"))
                        ->whereBetween('flow_histories.created_at', [$starttime, $endtime])
                        ->groupBy(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m/%d')"))
                        ->first();
                    $list['day'][0] = date('Y/m/d',$now);
                    $list['number']=[0];
                    if(empty($result)){
                        $list['number'][0]=0;
                    }else{
                        $list['number'][0]=$result->count;
                    }
                    return $list;
                    break;
                //30天
                case 3:
                    $endtime = time();
                    $starttime = $endtime - 24 * 3600 * 30;
                    $result = $queryss
                        ->select(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m/%d') as hours"), DB::raw("count(*) as count"))
                        ->whereBetween('flow_histories.created_at', [$starttime, $endtime])
                        ->groupBy(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m/%d')"))
                        ->get()
                        ->toarray();
                    for ($i = 0; $i < 30; $i++) {
                        $h = 29-$i;
                        $list['day'][$i] = date('Y/m/d', strtotime("-$h day"));
                        $list['number'][$i]=0;
                    }
                    if (!empty( $result)) {
                        foreach ( $result as $k) {
                            if (in_array($k->hours, $list['day'])) {
                                $key = array_search($k->hours,$list['day']);
                                $list['number'][$key] = $k->count;
                            }
                        }
                    }
                   $s = -1;
                    for ($i = 0; $i < 10; $i++) {
                        $h = 30 - $i * 3;
                        $k = 27 - $i * 3;
                        $listInfo['day'][$i] = date("m/d", strtotime("-$h day")) . '-' . date("m/d", strtotime("-$k day"));
                        $listInfo['number'][$i] = $list['number'][++$s] + $list['number'][++$s] + $list['number'][++$s];
                    }
                    return $listInfo;
                    break;
                //90天
                case 4:
                    $endtime = time();
                    $starttime = $endtime - 24 * 3600 * 90;
                    $result = $queryss
                        ->select(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m/%d') as hours"), DB::raw("count(*) as count"))
                        ->whereBetween('flow_histories.created_at', [$starttime, $endtime])
                        ->groupBy(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m/%d')"))
                      
                        ->get()
                        ->toarray();
                    for ($i = 0; $i < 90; $i++) {
                        $h = 89-$i;
                        $list['day'][$i] = date('Y/m/d', strtotime("-$h day"));
                        $list['number'][$i]=0;
                    }
                    if (!empty($result)) {
                        foreach ($result as $k) {
                            if (in_array($k->hours, $list['day'])) {
                                $key = array_search($k->hours,$list['day']);
                                $list['number'][$key] = $k->count;
                               
                            }
                        }
                    }
                    $s = -1;
                    for ($i = 0; $i < 10; $i++) {
                        $h = 90 - $i * 9;
                        $k = 81 - $i * 9;
                        $listInfo['day'][$i] = date("m/d", strtotime("-$h day")) . '-' . date("m/d", strtotime("-$k day"));
                        $listInfo['number'][$i] =
                            $list['number'][++$s] +   $list['number'][++$s] +
                            $list['number'][++$s] +   $list['number'][++$s] +
                            $list['number'][++$s] +    $list['number'][++$s] +
                            $list['number'][++$s] +  $list['number'][++$s] +
                            $list['number'][++$s];
                    }
                    return $listInfo;
                    break;
                //180天
                case 5:
                    $endtime = time();
                    $starttime = $endtime - 24 * 3600 * 180;
                    $result = $queryss
                        ->select(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m') as hours"), DB::raw("count(*) as count"))
                        ->whereBetween('flow_histories.created_at', [$starttime, $endtime])
                        ->groupBy(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m')"))
                        ->get()
                        ->toarray();
                    for ($i = 0; $i < 6; $i++) {
                        $h = 5-$i;
                        $list['day'][$i] = date('Y/m', strtotime("-$h month",$t));
                        $list['number'][$i]=0;
                    }
                    if (!empty($result)) {
                        foreach ($result as $k) {
                            if (in_array($k->hours, $list['day'])) {
                                $key = array_search($k->hours,$list['day']);
                                $list['number'][$key] = $k->count;
                            }
                        }
                    }
                    return $list;
                    break;
                //360天
                case 6:
                    $endtime = time();
                    $starttime = $endtime - 24 * 3600 * 360;
                    $result = $queryss
                        ->select(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m') as hours"), DB::raw("count(*) as count"))
                        ->whereBetween('flow_histories.created_at', [$starttime, $endtime])
                        ->groupBy(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m')"))
                        ->get()
                        ->toarray();
                    for ($i = 0; $i < 12; $i++) {
                        $h = 11-$i;
                        $list['day'][$i] = date('Y/m', strtotime("- $h month",$t));
                        $list['number'][$i]=0;
                    }
                    if (!empty($result)) {
                        foreach ($result as $k) {
                            if (in_array($k->hours, $list['day'])) {
                                $key = array_search($k->hours,$list['day']);
                                $list['number'][$key] = $k->count;
                            }
                        }
                    }
                    return $list;
                    break;
                //一周
                default:
                    $endtime = time();
                    $starttime = $endtime - 24 * 3600 * 7;
                    $result = $queryss
                        ->select(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m/%d') as hours"), DB::raw("count(*) as count"))
                        ->whereBetween('flow_histories.created_at', [$starttime, $endtime])
                        ->groupBy(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m/%d')"))
                        ->get()
                        ->toarray();
                    for ($i = 0; $i < 7; $i++) {
                        $h = 6-$i;
                        $list['day'][$i] = date('Y/m/d', strtotime("-$h day"));
                    }
                    $list['number'] = [0,0,0,0,0,0,0];
                    if (!empty($result)) {
                        foreach ($result as $k) {
                            if (in_array($k->hours, $list['day'])) {
                                $key = array_search($k->hours,$list['day']);
                                $list['number'][$key] = $k->count;
                            }
                        }
                    }
                    return $list;
                    break;
            }
    }
//    人员统计
   public function persional(Request $request){
       $userInfo = $this->user;
       $t = strtotime(date('Y-m',time()));
//        dd($userInfo);
       $query = DB::table('flow_histories')
           ->join('resumes','resumes.id','=','flow_histories.resume_id')
           ->leftJoin('positions','resumes.position_id','=','positions.id')
           ->where('flow_histories.company_id',$userInfo->company_id)
           ->where('resumes.status',0);
       $steps = $request->input('steps');
       $edu = $request->input('edu');
       $peopleId = $request->input('peopleId');
       $position = $request->input('position');
       $sex = $request->input('sex');
       if(!empty($steps)){
           if($steps==4){
               $query = $query->whereNotIn('flow_histories.step_id',[1,2,3]);
           }else{
               $query = $query->where('flow_histories.step_id',$steps);
           }
       }
       if(!empty($edu)){
           if($edu=='其他'){
               $query = $query->where(function($query){
                   $query = $query->whereNull('resumes.edu')->orWhere([['resumes.edu','!=','大专及以下'],['resumes.edu','!=','大专'],['resumes.edu','!=','本科'],['resumes.edu','!=','硕士'],['resumes.edu','!=','博士'],['resumes.edu','!=','博士及以上']]);
                   return $query;
               });
            
           }else{
               $query = $query->where('resumes.edu',$edu);
           }
       }
       if(!empty($sex)){
           if($sex=='其他'){
               $query = $query = $query->where('resumes.sex','');
           }else{
               $query = $query->where('resumes.sex',$sex);
           }
       }
       if(!empty($peopleId)){
           $query = $query->where('flow_histories.owner_id',$peopleId);
       }
       if(!empty($position)){
           $query = $query->where(['resumes.position_id'=>$position,'positions.status'=>0]);
       }
       $time = $request->input('time') ?? 2;
       $now = time();
      
           switch($time) {
               case 1:
                   //当天
                   $starttime = strtotime(date('Ymd'));
                   $endtime = time();
                   $result = $query
                       ->select(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m/%d') as hours"), DB::raw("count(*) as count"))
                       ->whereBetween('flow_histories.created_at', [$starttime, $endtime])
                       ->groupBy(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m/%d')"))
                       ->first();
                   $list['day'][0] = date('Y/m/d',$now);
                   $list['number']=[0];
                   if(empty($result)){
                       $list['number'][0]=0;
                   }else{
                       $list['number'][0]=$result->count;
                   }
                   return $list;
                   break;
               //30天
               case 3:
                   $endtime = time();
                   $starttime = $endtime - 24 * 3600 * 30;
                   $result = $query
                       ->select(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m/%d') as hours"), DB::raw("count(*) as count"))
                       ->whereBetween('flow_histories.created_at', [$starttime, $endtime])
                       ->groupBy(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m/%d')"))
                       ->get()
                       ->toarray();
                   for ($i = 0; $i < 30; $i++) {
                       $h = 29-$i;
                       $list['day'][$i] = date('Y/m/d', strtotime("-$h day"));
                       $list['number'][$i]=0;
                   }
                   if (!empty( $result)) {
                       foreach ( $result as $k) {
                           if (in_array($k->hours, $list['day'])) {
                               $key = array_search($k->hours,$list['day']);
                               $list['number'][$key] = $k->count;
                           }
                       }
                   }
                   $s = -1;
                   for ($i = 0; $i < 10; $i++) {
                       $h = 30 - $i * 3;
                       $k = 27 - $i * 3;
                       $listInfo['day'][$i] = date("m/d", strtotime("-$h day")) . '-' . date("m/d", strtotime("-$k day"));
                       $listInfo['number'][$i] = $list['number'][++$s] + $list['number'][++$s] + $list['number'][++$s];
                   }
                   return $listInfo;
                   break;
               //90天
               case 4:
                   $endtime = time();
                   $starttime = $endtime - 24 * 3600 * 90;
                   $result = $query
                       ->select(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m/%d') as hours"), DB::raw("count(*) as count"))
                       ->whereBetween('flow_histories.created_at', [$starttime, $endtime])
                       ->groupBy(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m/%d')"))
                       ->get()
                       ->toarray();
                   for ($i = 0; $i < 90; $i++) {
                       $h = 89-$i;
                       $list['day'][$i] = date('Y/m/d', strtotime("-$h day"));
                       $list['number'][$i]=0;
                   }
                   if (!empty($result)) {
                       foreach ($result as $k) {
                           if (in_array($k->hours, $list['day'])) {
                               $key = array_search($k->hours,$list['day']);
                               $list['number'][$key] = $k->count;
                            
                           }
                       }
                   }
                   $s = -1;
                   for ($i = 0; $i < 10; $i++) {
                       $h = 90 - $i * 9;
                       $k = 81 - $i * 9;
                       $listInfo['day'][$i] = date("m/d", strtotime("-$h day")) . '-' . date("m/d", strtotime("-$k day"));
                       $listInfo['number'][$i] =
                           $list['number'][++$s] +   $list['number'][++$s] +
                           $list['number'][++$s] +   $list['number'][++$s] +
                           $list['number'][++$s] +    $list['number'][++$s] +
                           $list['number'][++$s] +  $list['number'][++$s] +
                           $list['number'][++$s];
                   }
                   return $listInfo;
                   break;
               //180天
               case 5:
                   $endtime = time();
                   $starttime = $endtime - 24 * 3600 * 180;
                   $result = $query
                       ->select(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m') as hours"), DB::raw("count(*) as count"))
                       ->whereBetween('flow_histories.created_at', [$starttime, $endtime])
                       ->groupBy(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m')"))
                       ->get()
                       ->toarray();
                   for ($i = 0; $i < 6; $i++) {
                       $h = 5-$i;
                       $list['day'][$i] = date('Y/m', strtotime("-$h month",$t));
                       $list['number'][$i]=0;
                   }
                   if (!empty($result)) {
                       foreach ($result as $k) {
                           if (in_array($k->hours, $list['day'])) {
                               $key = array_search($k->hours,$list['day']);
                               $list['number'][$key] = $k->count;
                           }
                       }
                   }
                   return $list;
                   break;
               //360天
               case 6:
                   $endtime = time();
                   $starttime = $endtime - 24 * 3600 * 360;
                   $result = $query
                       ->select(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m') as hours"), DB::raw("count(*) as count"))
                       ->whereBetween('flow_histories.created_at', [$starttime, $endtime])
                       ->groupBy(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m')"))
                       ->get()
                       ->toarray();
                   for ($i = 0; $i < 12; $i++) {
                       $h = 11-$i;
                       $list['day'][$i] = date('Y/m', strtotime("- $h month",$t));
                       $list['number'][$i]=0;
                   }
                   if (!empty($result)) {
                       foreach ($result as $k) {
                           if (in_array($k->hours, $list['day'])) {
                               $key = array_search($k->hours,$list['day']);
                               $list['number'][$key] = $k->count;
                           }
                       }
                   }
                   return $list;
                   break;
            
               //一周
               default:
                   $endtime = time();
                   $starttime = $endtime - 24 * 3600 * 7;
                   $result = $query
                       ->select(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m/%d') as hours"), DB::raw("count(*) as count"))
                       ->whereBetween('flow_histories.created_at', [$starttime, $endtime])
                       ->groupBy(DB::raw("from_unixtime(flow_histories.created_at,'%Y/%m/%d')"))
                       ->get()
                       ->toarray();
                   for ($i = 0; $i < 7; $i++) {
                       $h = 6-$i;
                       $list['day'][$i] = date('Y/m/d', strtotime("-$h day"));
                   }
                   $list['number'] = [0,0,0,0,0,0,0];
                   if (!empty($result)) {
                       foreach ($result as $k) {
                           if (in_array($k->hours, $list['day'])) {
                               $key = array_search($k->hours,$list['day']);
                               $list['number'][$key] = $k->count;
                           }
                       }
                   }
                   return $list;
                   break;
           }
   }
//   人员接口
    public function allPeople(Request $request){
        $userInfo = $this->user;
        $list = DB::table('staff')
            ->join('users', 'staff.mobile', '=', 'users.mobile')
            ->select('users.id','staff.name')
            ->where(['staff.company_id'=>$userInfo->company_id,'staff.status'=>0])
            ->get();
        return response()->json(['code' => 200, 'msg' => '操作成功', 'data' =>$list]);
    }
//    趋势图
    public function trendChart(Request $request){
        $type = $request->input('type');
        if($type ==1){
            $result = $this->trendFlow($request);
        }else if($type ==2){
          
            $result = $this->position($request);
        }else if($type ==3){
            $result = $this->persional($request);
        }else{
            $result =[];
        }
        return response()->json(['code' => 200, 'msg' => '操作成功', 'data' =>$result]);
    }
}
